package org.rainy.gift.exception;

import cn.hutool.core.util.StrUtil;

import java.util.Collection;
import java.util.Objects;

/**
 * 断言工具类
 * 什么是断言? 当断言一个对象为true的时候,那么当它为false的时候就要抛出异常
 * 在断言工具类里面,就可以把一些常见的判断,都写在这个断言工具类
 */
public class RonghuaAssert {
    /**
     * lang3包的isBlank和isEmpty的区别??
     *   isNotBlank 不仅要判断字符串是否为空,而且还要判断长度是否大于0,还要判断字符串里面的每个字符是否是空字符
     *   isNotEmpty 只判断字符串是否为空,且长度是否大于0
     *   String str = "       ";
     *   isNotEmpty()   true
     *   isNotBlank()   false
     */


    /**
     * 断言一个字符串不为空,那么字符串为空的时候就要抛出异常
     */
    public static void isNotBlank(String str,String message){
        if(StrUtil.isBlank(str)){
            throw new BusinessException(message);
        }
    }

    /**
     * 断言字符串为空,则不为空就抛出异常
     * @param str
     * @param message
     */
    public static void isBlank(String str,String message){
        if(StrUtil.isNotBlank(str)){
            throw new BusinessException(message);
        }
    }

    // 断言对象为空
    public static void isNull(Object obj,String message){
        if(Objects.nonNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,String message){
        if(Objects.isNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言集合为空
    public static void isNull(Collection collection, String message){
        if(collection != null && collection.size() > 0){
            throw new BusinessException(message);
        }
    }

    // 断言集合不为空
    public static void isNotNull(Collection collection, String message){
        if(collection == null || collection.size() == 0){
            throw new BusinessException(message);
        }
    }

    // 断言两个字符串相等
    public static void isEq(String str1,String str2,String message){
        if(!str1.equals(str2)){
            throw new BusinessException(message);
        }
    }

    //============================================================//

    public static void isNotBlank(String str,ResponseCode responseCode){
        if(StrUtil.isBlank(str)){
            throw new BusinessException(responseCode);
        }
    }

    /**
     * 断言字符串为空,则不为空就抛出异常
     * @param str
     */
    public static void isBlank(String str,ResponseCode responseCode){
        if(StrUtil.isNotBlank(str)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言对象为空
    public static void isNull(Object obj,ResponseCode responseCode){
        if(Objects.nonNull(obj)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,ResponseCode responseCode){
        if(Objects.isNull(obj)){
            throw new BusinessException(responseCode);
        }
    }

    // 断言集合为空
    public static void isNull(Collection collection, ResponseCode responseCode){
        if(collection != null && collection.size() > 0){
            throw new BusinessException(responseCode);
        }
    }

    // 断言集合不为空
    public static void isNotNull(Collection collection, ResponseCode responseCode){
        if(collection == null || collection.size() == 0){
            throw new BusinessException(responseCode);
        }
    }

    public static void isEq(String str1,String str2,ResponseCode responseCode){
        if(!str1.equals(str2)){
            throw new BusinessException(responseCode);
        }
    }

    public static void isEqIgnoreCase(String str1,String str2,ResponseCode responseCode){
        if(!str1.equalsIgnoreCase(str2)){
            throw new BusinessException(responseCode);
        }
    }

    public static void isTrue(Boolean flag,ResponseCode responseCode){
        if(!flag){
            throw new BusinessException(responseCode);
        }
    }
}
